package com.hb.game.pk.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hb.game.pk.R;
import com.hb.game.pk.adpter.ContactListAdapter;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseFragment;
import com.hb.game.pk.beans.FriendBean;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.ui.activity.SearchActivity;
import com.hb.game.pk.ui.activity.contact.TransActivity;
import com.hb.game.pk.utils.ToastUtils;
import com.hb.game.pk.widgets.titlebar.TitleBar;
import com.google.gson.Gson;


import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class ContactFragment extends BaseFragment<FriendBean> {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    List<FriendBean.DataBean> data;
    int p = 1;
    private List<FriendBean.DataBean> moreData;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_contact;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return R.id.rv_list;
    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        requestNet(false);
    }

    private void requestNet(final boolean loadMore) {
        call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).friendList(p);
        call.enqueue(new Callback<FriendBean>() {
            @Override
            public void onResponse(Call<FriendBean> call, Response<FriendBean> response) {
                FriendBean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }

                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                if (loadMore) {
                    moreData = body.getData();
                    loadMoreSuccess();
                    if (moreData != null && moreData.size() > 0) {
                        p++;
                    } else {
                        loadMoreEnd();
                        p = 1;
                    }
                } else {
                    data = body.getData();
                    initAdapter();
                    p++;
                }
            }

            @Override
            public void onFailure(Call<FriendBean> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
                if (loadMore) {
                    loadMoreFail();
                }
            }
        });
    }

    public void setMoreData() {
        if (moreData != null)
            mAdapter.addData(moreData);
    }

    @Override
    protected void onLoadMore() {
        super.onLoadMore();
        requestNet(true);
    }

    @Override
    protected void initAdapter() {
        if (mAdapter == null) {
            mAdapter = new ContactListAdapter(R.layout.item_contact_list);
            mAdapter.setNewData(data);
            mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    List<FriendBean.DataBean> data = (List<FriendBean.DataBean>) adapter.getData();
                    FriendBean.DataBean dataBean = data.get(position);
                    Intent intent = new Intent(mActivity, TransActivity.class);
                    intent.putExtra("friend", new Gson().toJson(dataBean));
                    startActivity(intent);
                }
            });
        } else {
            mAdapter.setNewData(data);
        }
        super.initAdapter();
    }

    @OnClick(R.id.ll_search)
    public void onViewClicked() {
        startActivity(new Intent(mActivity, SearchActivity.class));
    }
}
